#!/bin/bash
##################################################################
## function:chean_api_response  clean  original_api_response_log to api_response_log table
## eg：bash chean_api_response.sh 20150505
## author: shx
## createdate: 20150608 
## version:     
################################################################## 
. /home/hadoop/shx/baseUtil.sh


if [[ ! -n "$1" ]] && [[ ! -n "$2" ]] ;then
	HISTORYDATE=`date -d '-1 hours'  +%Y%m%d`
	HOUR=`date -d '-1 hours' +%H`
else
	HISTORYDATE=$1
	HOUR=$2
fi


echo `date`"==========clean api_response_log start===============" 
/usr/local/cloud/hive/bin/hive -e  " 
use qyer;
ALTER TABLE original_api_response_log DROP PARTITION(logdate='$HISTORYDATE',loghour='$HOUR');
ALTER TABLE original_api_response_log ADD PARTITION (logdate='$HISTORYDATE' ,loghour='$HOUR') location '/flume/original/nobita/$HISTORYDATE/$HOUR';

ALTER TABLE api_response_log DROP PARTITION(logdate='$HISTORYDATE' , loghour='$HOUR');
ALTER TABLE api_response_log ADD PARTITION (logdate='$HISTORYDATE' ,loghour='$HOUR') ;


delete file /home/hadoop/shx/clean_api_response/tomaps.py;
add file /home/hadoop/shx/clean_api_response/tomaps.py;
insert overwrite table api_response_log partition (logdate = '$HISTORYDATE' ,loghour='$HOUR') 
SELECT  
  TRANSFORM (line)  
  USING 'python tomaps.py'  
  AS (uid string, 
    guid string, 
    times string, 
    platform string,
    appname string ,
    route string,
    resultlist array<String> , 
    apicontent map<string,string>,
    conditions map<string,String> ,
    context string ,
    requestId string , 
    strategy string  , 
    logtime string)  
FROM original_api_response_log 
where logdate='$HISTORYDATE' and loghour='$HOUR'
;
"

if [ $? -eq 0 ]
  then
  	echo `date`"=============== clean api_response_log is ok  end===============" 
else
  	echo `date`"================clean api_response_log is error end !================" 
  	mailSend "127 :clean api_response_log is error end !"
fi








